{-# OPTIONS -Wall #-}

module Demo04 where


import Majalan.Base
import Majalan.Base.Melody        -- temp
import qualified Majalan.Base.NoteList as NL       -- temp

import Majalan.Pitch.ET12
import qualified Majalan.Pitch.Centaur as C



import Data.Monoid

import Prelude hiding ( replicate )

-- TODO - dont use zak for this one...
-- Also, seem to be saturating zak channel, producing an 
-- extraneous whistling tone.


data Orch = WG_BOWED_BAR | ZAKOUT
  deriving (Eq,Ord,Show)

instance CsOrch Orch where
  instNumber WG_BOWED_BAR = 1
  instNumber ZAKOUT       = 2


-- | Bowed bar should have constant width.
wgBowedBar :: Decimal -> Decimal -> Decimal -> EventFw uctx ()
wgBowedBar iamp ipch iconst = 
    let izakout = 1 in
    makeEventFw WG_BOWED_BAR () 1.0 (const [ VFloat iamp
                                           , VFloat ipch
                                           , VFloat iconst
                                           , VInt   izakout
                                           ])
                              

mkBBEvent :: ET12 -> EventFw uctx ()
mkBBEvent pch = wgBowedBar 0.9 (toHertz 440 pch) 0


zakout :: Processor
zakout = 
    let izakin = 1 in makeProcessor ZAKOUT 2.0 [ VInt izakin ]





--
-- WARNING 
-- =======
-- wgbowedbar seems very sensitive to kamp(litude)
-- (first input param).
-- 
-- To get a range of amplitudes you would have to post-process
-- (scale) the signal. Remember though, that many physical 
-- instruments are extremely limited in their amplitude range.
-- Expecting all instruments to go from ppp to fff is 
-- unrealistic.
--


type MyCtx = ()

ctx_zero :: MyCtx
ctx_zero = ()


score01 :: ScoreObj
score01 = score default_timeline ctx_zero $ do 
    pt1 <- part $ localize (set_metered (2,4) 120) $ do 
               { eventListPos (1,1) $ bb_play
               }
    bracket pt1 zakout
  where
    bb_play = melodyFw melody1 imap1 $ \pch _ -> mkBBEvent pch


melody1 :: Melody ET12 
melody1 = arpeggioNotes arpegg1

arpegg1 :: Arpeggio ET12
arpegg1 = let ps = chordNotes $ octaveDouble $ dim_9 $ minor_triad c_3
          in arpeggio ps (up <> up <> down <> up)
                   


imap1 :: ImpulseMap ()
imap1 = beatPattern (2,4) 120 $ bar1 <> bar2 <> bar3 <> bar3 <> bar3
  where
    bar1 = divisions [ 2,1,1,          1,1,2 ]
    bar2 = divisions [ 1,1,1,1,        1,1,2 ]
    bar3 = arbitrary [ 0.25, 0.25, 0.25, 0.25 ]

col_specs :: ColumnSpecs
col_specs = columnSpecs [ (WG_BOWED_BAR, cols_bowed_bar) 
                        , (ZAKOUT,       cols_zakout)
                        ]
  where             
    cols_bowed_bar  = [ defaultFormat "amp", defaultFormat "pch"
                      , defaultFormat "const", defaultFormat "zout" ]
    cols_zakout     = [ defaultFormat "zakin" ]




demo01 :: IO ()
demo01 = shellTest odac "orc/wgbowedbar.orc" $ renderScoreObj col_specs score01

